package com.zlw.netty.websocket;

import io.netty.channel.ChannelInitializer;
import io.netty.channel.ChannelPipeline;
import io.netty.channel.socket.SocketChannel;
import io.netty.handler.codec.http.HttpObjectAggregator;
import io.netty.handler.codec.http.HttpServerCodec;
import io.netty.handler.codec.http.websocketx.WebSocket00FrameDecoder;
import io.netty.handler.codec.http.websocketx.WebSocketServerProtocolHandler;
import io.netty.handler.stream.ChunkedWriteHandler;

/**
 * @ClassName WSServerInit
 * @Description TODO
 * @Author zhaoluowei
 * @Date 2020/1/18  0:16
 * @Version 1.0
 */

public class WSServerInit extends ChannelInitializer<SocketChannel> {
    @Override
    protected void initChannel(SocketChannel channel) throws Exception {
        ChannelPipeline pipeline = channel.pipeline();

        //编解码器
        pipeline.addLast(new HttpServerCodec());

        //大数据量的处理
        pipeline.addLast(new ChunkedWriteHandler());

        //一个消息的长度
        pipeline.addLast(new HttpObjectAggregator(1024 * 64));

        //============================= 以上是对 http协议的 支持 ===================================

        //websocket 服务器处理的协议，用于指定给客户端连接访问的路由
        pipeline.addLast(new WebSocketServerProtocolHandler("/ws"));

        //自定义的handler
        pipeline.addLast(new ChatHandler());
    }
}
